<?php


namespace App\Models\CacheModel\chat\hz;


use app\common\lib\Util;
use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class HzTag extends BaseCache
{
    protected $table = 'haozhungpt_api.hz_tag';

    protected $cacheModel = 'chat.tag';

    /**
     * 获取所有标签
     * @return array|mixed
     * @throws \Exception
     */
    public function allTag()
    {
        $ids = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->ZsGetIocData('list', function ()
        {
            $where = ['status' => ['neq', 0]];
            try
            {
                $ids = $this->where($where)->order('sort', 'DESC')->column('id', 'id');

                $ids = array_flip_key_value($ids);
            }
            catch (\Exception $e)
            {
                $ids = [];
            }

            return $ids;
        });
        if (Util::CheckAppVersion('2.10.4') !== '小')
        {
            $ids[] = 11;    // 合盘
        }
        return $this->batchGetInfoByIds($ids);
    }

    /**
     * 获取所有标签并重组
     * @param $data
     * @return array
     */
    public function getQuestionAllsResrt($data): array
    {
        $resData = [];
        foreach ($data as $value)
        {
            if (!isset($resData[$value['flag']]))
            {
                $resData[$value['flag']] = [
                    'level' => (int)$value['flag'],
                    'tags' => [],
                ];
            }
            $resData[$value['flag']]['tags'][] = [
                'id' => (int)$value['id'],
                'tname' => $value['tag_name'],
            ];
        }
        ksort($resData);
        return $resData ? array_values($resData) : $resData;
    }

    /**
     * 获取对应的问题评论信息
     * @param array $ids
     * @param array|null $type
     * @param string $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $where = ['id' => ['in', $ids]];
        try
        {
            $data = $this
                ->where($where)
                ->order('sort desc,id desc')
                ->column('*', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        foreach ($resData as $orderId => $orderItemData)
        {
            $resData[$orderId] = arrayKeyData2int($orderItemData, ['id', 'tag_type', 'sort', 'status', 'create_time', 'is_recommend']);
        }

        return $resData;
    }
}
